Ext.BLANK_IMAGE_URL = '../ext/resources/images/default/s.gif';
Ext.ux.EquipmentList = Ext.extend(Ext.Panel, {
	border:false,
	layout : 'border',
	title:'设备列表',
	closable : 'true',
	initComponent : function() {
		var basePath = document.getElementById("basePath").href;
		var equipmentListUrl = basePath + 'equipment/list.action?visible=true';
		var limit = 20;
	
		var equipmentStore = new Ext.data.Store({
			storeId:'equipmentStore',
			proxy:new Ext.data.HttpProxy({url: equipmentListUrl}),
		    reader : new Ext.data.JsonReader({
			    idProperty: 'id',       
			    root: 'equipmentList',
			    totalProperty: 'equipmentTotal',
			    remoteSort: true,
			    
			    fields: [
			        {name: 'id', mapping:'id', type:'int'},
			        {name: 'name', mapping:'name', type:'string'},
			        {name: 'model', mapping:'model', type:'string'},
			        {name: 'count', mapping:'count', type:'int'},
			        {name: 'buyTime', mapping:'buyTime', type:'date'},
			        {name: 'category', mapping:'category.name', type:'string'},
			        {name: 'lang', mapping:'lang', type:'string'},
			        {name: 'createDate', mapping:'createDate', type:'date'}
			    ]    
			})
		});
		
		var langStore = new Ext.data.ArrayStore({
	        fields: ['lang', 'value'],
	        data : [['简体中文', 'zh_CN'],['繁体中文', 'zh_TW'],['USA English', 'en_US']]
	    });
		
		var category = new Ext.data.Store( {
			proxy: new Ext.data.HttpProxy({url: basePath + 'base/list.action?whoCategory=equipment'}),
			reader:new Ext.data.ArrayReader({idIndex: 0},
				Ext.data.Record.create([{name: 'id'}, {name: 'name'}]))
		});
		
		this.items = [ new Ext.form.FormPanel({
			title : '查找选项',
			id:'searchEquipmentForm',
			region : 'west',
			layout : 'vbox',
			collapsible : true,
			width : 200,
			maxWidth : 200,
			minWidth : 200,
			frame : true,
			split : true,
			layoutConfig : {align : 'stretch'},
			items : [ {
				xtype : 'displayfield',
				value : '查找条件，名称：',
				margins : '0 0 0 5',
				width : 100
			}, {
				xtype : 'textfield',
				id:'equipmentName'
			}, {
				xtype : 'displayfield',
				value : '时间：从',
				margins : '0 0 0 5',
				width : 50
			}, {
				xtype : 'datefield',
				id:'fromDateEquipment',
				editable:false,
				format:'Y-m-d'
			}, {
				xtype : 'displayfield',
				value : '到',
				margins : '0 5 0 5',
				width : 15
			}, {
				xtype : 'datefield',
				id:'toDateEquipment',
				editable:false,
				format:'Y-m-d'
			}, {
				xtype : 'displayfield',
				value : '选择语言',
				margins : '0 5 0 5'
			}, {
				xtype : "combo",
				store : langStore,
				mode : 'local',
				valueField: 'value',
				id:'langEquipment',
				displayField : 'lang',
				allowBlank : false,
				typeAhead : true,
				triggerAction : 'all',
				editable:false,
				forceSelection : true,
				selectOnFocus : true,
				listeners:{
					select : function(combo, record, index) {
						category.removeAll();
						category.proxy.setUrl(basePath + 'base/list.action?whoCategory=equipment' +
							'&lang=' + record.get('value'));
						category.load();
					}
				}
			}, {
				xtype : 'displayfield',
				value : '类别：',
				width : 40,
				margins : '0 5 0 5'
			}, {
				xtype : "combo",
				id:'categoryEquipment',
				store : category,
				displayField : 'name',
				typeAhead : true,
				mode : 'local',
				editable:false,
				forceSelection : true,
				triggerAction : 'all',
				selectOnFocus : true
			}, {
				xtype : 'button',
				text : '搜索',
				listeners:{
					click:function(btn, event) {
						var params = Ext.getCmp('searchEquipmentForm').getForm().getValues();
						var searchUrl = basePath + 'equipment/search.action?equipmentName=' + 
							params.equipmentName + '&fromDate=' + params.fromDateEquipment + 
							'&toDate=' + params.toDateEquipment + '&category=' + params.categoryEquipment + 
							'&visible=true&lang=' + params.langEquipment;
						equipmentStore.proxy.setUrl(searchUrl);
						equipmentStore.load({params:{start:0, limit:limit}});
					}
				}
			}, {
				xtype : 'button',
				text : '显示全部',
				margins : '0 5 0 5',
				listeners:{
					click:function(btn, event) {
						equipmentStore.proxy.setUrl(equipmentListUrl);
						equipmentStore.load({params:{start:0, limit:limit}});
					}
				}
			} ]
		}), {
			xtype : 'grid',
			title : '设备列表',
			id : 'equipmentDataGrid',
			region : 'center',
			split : true,
			frame : true,
			loadMask: true,
			sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
			autoScroll:true,
			ds : equipmentStore,
			columns : [ {
				xtype : 'gridcolumn',
				dataIndex : 'id',
				header : '序号',
				sortable : true,
				width : 40
			}, {
				xtype : 'gridcolumn',
				dataIndex : 'name',
				header : '设备名称',
				sortable : true,
				width : 160,
				align : 'left'
			}, {
				xtype : 'gridcolumn',
				dataIndex : 'model',
				header : '型号',
				sortable : true,
				width : 100
			}, {
				xtype : 'gridcolumn',
				dataIndex : 'count',
				header : '数量',
				sortable : true,
				width : 100
			}, {
				xtype : 'gridcolumn',
				dataIndex : 'category',
				header : '类别',
				sortable : true,
				width : 100
			}, {
				xtype : 'gridcolumn',
				dataIndex : 'lang',
				header : '语言',
				sortable : true,
				width : 100
			}, {
				xtype : 'gridcolumn',
				dataIndex : 'model',
				header : '购买时间',
				sortable : true,
				width : 100
			}, {
				xtype : 'gridcolumn',
				dataIndex : 'createDate',
				format:'Y-m-d h:m:s',
				header : '添加日期',
				sortable : true,
				width : 140
			}],
			bbar: new Ext.PagingToolbar({
	            pageSize: limit,
	            store: equipmentStore,
	            beforePageText:"当前第",   
	            afterPageText:"页，共{0}页",   
	            lastText:"尾页",   
	            nextText :"下一页",   
	            prevText :"上一页",   
	            firstText :"首页",   
	            refreshText:"刷新页面",   
	            displayInfo: true,
	            displayMsg: '显示第 {0} 条到 {1} 条记录，共 {2} 条',
	            emptyMsg: "没有记录"
	        }),
	        tbar:[{
	        	text:'修改',
	        	tooltip:'Modify A Record',
	        	iconCls:'modify',
	        	handler:function() {
	        		var linenum = Ext.getCmp("equipmentDataGrid").getSelectionModel().lastActive;
	        		if(getType(linenum) == "number") {
		        		new Ext.ux.ModifyEquipment({id:'modifyEquipmentWin'}).show(this);
	        		} else {
	        			Ext.Msg.alert('警告', '未选中任何行');
	        		}
	        	}
	        }, '-', {
	        	text:'查看',
	        	tooltip:'View A Record',
	        	iconCls:'open',
	        	handler:function() {
	        		var linenum = Ext.getCmp("equipmentDataGrid").getSelectionModel().lastActive;
	        		if(getType(linenum) == "number") {
		        		new Ext.ux.ViewEquipment({idString:'equipmentDataGrid'}).show(this);
	        		} else {
	        			Ext.Msg.alert('警告', '未选中任何行');
	        		}
	        	}
	        }, '-', {
	        	xtype: 'tbsplit',
	        	tooltip:'Delete A Record',
            	text: '删除',
            	iconCls:'delete',
           		menu: new Ext.menu.Menu({
                	items: [{
                		text: '彻底删除',
                		handler:function() {
                			var linenum = Ext.getCmp("equipmentDataGrid").getSelectionModel().lastActive;
                			var linedata = Ext.getCmp("equipmentDataGrid").getStore().getAt(linenum);
                			if(getType(linenum) == "number") {
				        		ajaxDeleteXxx(linedata.json.id, 'equipment', 'delete');
			        		} else {
			        			Ext.Msg.alert('警告', '未选中任何行');
			        		}
                		}
                	},{
                		text: '可恢复删除',
                		handler:function() {
                			var linenum = Ext.getCmp("equipmentDataGrid").getSelectionModel().lastActive;
                			var linedata = Ext.getCmp("equipmentDataGrid").getStore().getAt(linenum);
                			if(getType(linenum) == "number") {
				        		ajaxDeleteXxx(linedata.json.id, 'equipment', 'update');
			        		} else {
			        			Ext.Msg.alert('警告', '未选中任何行');
			        		}
                		}
                	}]
                })
	        }]
		} ];
		Ext.ux.EquipmentList.superclass.initComponent.call(this);
		equipmentStore.load({params:{start:0, limit:limit}});
	}
});
Ext.reg('lgnEquipmentList', Ext.ux.EquipmentList);